package com.lanyan.system.controller.system;

import java.math.BigDecimal;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.service.IAccountService;
import com.lanyan.system.service.ICourierService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import com.lanyan.system.domain.Opinion;
import com.lanyan.system.service.IOpinionService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 投诉建议Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/system/opinion")
public class OpinionController extends BaseController
{
    @Autowired
    private IOpinionService opinionService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private ICourierService courierService;

    /**
     * 查询投诉建议列表
     */
    @PreAuthorize("@ss.hasPermi('system:opinion:list')")
    @GetMapping("/list")
    @ApiImplicitParams({
          @ApiImplicitParam(name = "opinionId", value = "ID", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "content", value = "反馈内容", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "img", value = "凭证", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "phone", value = "联系方式", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "feedback", value = "反馈内容", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
          @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),

    })
    public TableDataInfo list(Opinion opinion)
    {
        startPage();
        List<Opinion> list = opinionService.selectOpinionList(opinion);
        for (Opinion item : list) {
            if (StringUtils.equals(item.getType(),"0")){
                item.setAccount(accountService.getById(item.getAccountId()));
            }else {
                item.setAccount(courierService.getById(item.getAccountId()));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出投诉建议列表
     */
    @PreAuthorize("@ss.hasPermi('system:opinion:export')")
    @Log(title = "投诉建议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Opinion opinion)
    {
        List<Opinion> list = opinionService.selectOpinionList(opinion);
        ExcelUtil<Opinion> util = new ExcelUtil<Opinion>(Opinion.class);
        util.exportExcel(response, list, "投诉建议数据");
    }

    /**
     * 获取投诉建议详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:opinion:query')")
    @GetMapping(value = "/{opinionId}")
    public AjaxResult getInfo(@PathVariable("opinionId") Long opinionId)
    {
        return success(opinionService.selectOpinionByOpinionId(opinionId));
    }

    /**
     * 新增投诉建议
     */
    @PreAuthorize("@ss.hasPermi('system:opinion:add')")
    @Log(title = "投诉建议", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "opinionId", value = "ID", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "content", value = "反馈内容", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "img", value = "凭证", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "phone", value = "联系方式", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "feedback", value = "反馈内容", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult add(@RequestBody Opinion opinion)
    {
        return toAjax(opinionService.insertOpinion(opinion));
    }

    /**
     * 修改投诉建议
     */
    @PreAuthorize("@ss.hasPermi('system:opinion:edit')")
    @Log(title = "投诉建议", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "opinionId", value = "ID", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "content", value = "反馈内容", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "img", value = "凭证", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "phone", value = "联系方式", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "feedback", value = "反馈内容", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult edit(@RequestBody Opinion opinion)
    {
        return toAjax(opinionService.updateOpinion(opinion));
    }

    /**
     * 删除投诉建议
     */
    @PreAuthorize("@ss.hasPermi('system:opinion:remove')")
    @Log(title = "投诉建议", businessType = BusinessType.DELETE)
	@DeleteMapping("/{opinionIds}")
    public AjaxResult remove(@PathVariable Long[] opinionIds)
    {
        return toAjax(opinionService.deleteOpinionByOpinionIds(opinionIds));
    }
}
